package com.example.controller;

import com.example.dto.ColonyHousePageDto;
import com.example.entity.ColonyHouse;
import com.example.entity.Page;
import com.example.entity.Result;
import com.example.service.ColonyHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("colonyhouse")
public class ColonyHouseController {

    @Autowired
    private ColonyHouseService colonyHouseService;

    @PostMapping("list")
    public Result getAllColonyHouse(@RequestBody ColonyHousePageDto colonyHousePageDto) {
        Result result = new Result();
        Page page = colonyHousePageDto.getPage();
        ColonyHouse colonyHouse = colonyHousePageDto.getColonyHouse();

        if (page != null) {
            int pageHelper = (page.getCurrentPage() - 1) * page.getPageSize();
            page.setCurrentPage(pageHelper);
        }
        List<ColonyHouse> colonyHouseList = colonyHouseService.findAllColonyHouse(page, colonyHouse);
        List<ColonyHouse> colonyHouses = colonyHouseService.findAll(colonyHouse);

        if (colonyHouseList != null) {
            result.setCode(1);
            result.setMsg("查询成功!");
            result.setColData(colonyHouseList);
            result.setTotal(colonyHouses.size());
        } else {
            result.setCode(0);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @PostMapping("del")
    public Result delColonyHouse(@RequestBody ColonyHouse colonyHouse) {
        Result result = new Result();
        int i = colonyHouseService.delColonyHouse(colonyHouse);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("禁用成功!");
        } else {
            result.setCode(0);
            result.setMsg("禁用失败!");
        }
        return result;
    }

    @PostMapping("edit")
    public Result editColonyHouse(@RequestBody ColonyHouse colonyHouse) {
        Result result = new Result();
        int i = colonyHouseService.updateColonyHouse(colonyHouse);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("修改成功!");
        } else {
            result.setCode(0);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @PostMapping("delete")
    public Result deleteColonyHouse(@RequestBody ColonyHouse colonyHouse) {
        Result result = new Result();
        int i = colonyHouseService.deleteColonyHouse(colonyHouse);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("禁用成功!");
        } else {
            result.setCode(0);
            result.setMsg("禁用失败!");
        }
        return result;
    }

    @PostMapping("add")
    public Result addColonyHouse(@RequestBody ColonyHouse colonyHouse) {
        Result result = new Result();
        int i = colonyHouseService.addColonyHouse(colonyHouse);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("添加成功!");
        } else {
            result.setCode(0);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @PostMapping("search")
    public Result searchColonyHouse(@RequestBody ColonyHousePageDto colonyHousePageDto) {
        Result result = new Result();
        Page page = colonyHousePageDto.getPage();
        ColonyHouse colonyHouse = colonyHousePageDto.getColonyHouse();

        if (page != null) {
            int pageHelper = (page.getCurrentPage() - 1) * page.getPageSize();
            page.setCurrentPage(pageHelper);
        }
        List<ColonyHouse> colonyHouseList = colonyHouseService.searchColonyHouse(page, colonyHouse);
        List<ColonyHouse> allColonyHouse = colonyHouseService.searchAllColonyHouse(colonyHouse);

        if (colonyHouseList != null) {
            result.setCode(1);
            result.setMsg("查询成功,有" + allColonyHouse.size() + "个结果!");
            result.setColData(colonyHouseList);
            result.setTotal(allColonyHouse.size());
        } else {
            result.setCode(0);
            result.setMsg("查询结果为空!");
        }
        return result;
    }
}
